package com.cloud.code.真题;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Demo14 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int start = Integer.parseInt(scanner.nextLine());
            int n = Integer.parseInt(scanner.nextLine());
            String[] strings = new String[n];
            for (int i = 0; i < strings.length; i++) {
                strings[i] = scanner.nextLine();
            }

            List<Integer> list = new ArrayList<>();
            StringBuilder stringBuilder = new StringBuilder(strings[start]);
            list.add(start);
            String end = strings[start].substring(strings[start].length()-1);
            while (true) {
                String str = null;
                int index = -1;
                for (int i = 0; i < strings.length; i++) {
                    boolean contains = list.contains(i);
                    if (contains) {
                        continue;
                    }

                    String tem = strings[i];
                    if (tem.startsWith(end)) {
                        if (str == null) {
                            str = tem;
                            index = i;
                        } else {
                            if (str.length() < tem.length()) {
                                str = tem;
                                index = i;
                            } else if (str.length() == tem.length()) {
                                if(str.compareTo(tem) >0){
                                    str = tem;
                                    index = i;
                                }
                            }
                        }
                    }
                }

                if(str == null){
                    break;
                }else {
                    stringBuilder.append(str);
                    list.add(index);
                    end = str.substring(str.length()-1);
                }
            }

            System.out.println(stringBuilder);

        }
    }
}
